/*
 * 软件著作权：北京康兰尚德数据科技有限公司
 * 项目名称：康兰数据购物中心分析平台
 *
 * NAME : StoreDaoImpl.java
 *
 * PURPOSE : 店铺数据库访问
 *
 * AUTHOR : EEDATA/ywb
 *
 *
 * 创建日期: 2013-11-25
 * HISTORY：
 * 变更日期 
 */
package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pojo.EcpGwcShopping;
import pojo.EcpGwcStore;
import pojo.ShopSort;
import util.HibernateUtils;

/**
 * 店铺表数据访问层
 * 
 * @author ywb
 */

public class StoreDaoImpl {
	public static double DEF_PI180 = 0.01745329252; // PI/180.0
	public static double DEF_R = 6370693.5; // radius of earth

	/**
	 * 根据购物中心id获取分类
	 */
	@SuppressWarnings("unchecked")
	public List<String> selectStoreSort(String shopid) {// 九、getShoppingCenterShopInfoList
		Session session = null;
		Transaction ts = null;
		List<String> list = null;
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("select distinct sort from EcpGwcStore where shopid=?");
			query.setParameter(0, shopid);
			list = query.list();

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return list;

	}

	/**
	 * 根据分类获取所在分类店铺信息
	 */
	@SuppressWarnings("unchecked")
	public List<EcpGwcStore> selectStoreBySort(String str) {// 九、getShoppingCenterShopInfoList
		Session session = null;
		Transaction ts = null;
		List<EcpGwcStore> list = null;
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session.createQuery("from EcpGwcStore where sort=?");
			query.setParameter(0, str);
			list = query.list();

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return list;

	}

	/**
	 * 根据分类获取分类名称
	 */
	@SuppressWarnings("unchecked")
	public String selectSortName(String sort) {// 九、getShoppingCenterShopInfoList
		Session session = null;
		Transaction ts = null;
		String str = "";
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("select dicname from EcpDicDetail where dicid=?");
			query.setParameter(0, sort);
			List<String> list = query.list();
			if (list.size() != 0)
				str = list.get(0);
			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return str;
	}

	/**
	 * 根据品牌id获取购物中心id
	 */
	@SuppressWarnings("unchecked")
	public List<String> selectShopid(String brandid) {// 三、getBrandShopInfoList接口
		Session session = null;
		Transaction ts = null;
		List<String> list = null;
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("select distinct shopid from EcpGwcStore where brandid=?");
			query.setParameter(0, brandid);
			list = query.list();

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return list;
	}

	/**
	 * 根据购物中心id获取购物中心信息
	 */
	public EcpGwcShopping selectShop(String shopid) {// 三、getBrandShopInfoList接口
		Session session = null;
		Transaction ts = null;
		EcpGwcShopping shop = null;
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			shop = (EcpGwcShopping) session.get(EcpGwcShopping.class, shopid);

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return shop;

	}

	/**
	 * 根据购物中心id获取区域id
	 */
	@SuppressWarnings("unchecked")
	public String selectTerritoryid(String shopid) {// 三、getBrandShopInfoList接口
		Session session = null;
		Transaction ts = null;
		String territoryid = "";
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("select territoryid from EcpGwcShopping where shopid=?");
			query.setParameter(0, shopid);
			List<String> list = query.list();

			if (list.size() != 0)
				territoryid = list.get(0);

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return territoryid;
	}

	/**
	 * 根据区域id获取城市名称
	 */
	@SuppressWarnings("unchecked")
	public String selectCity(String territoryid) {// 三、getBrandShopInfoList接口
		Session session = null;
		Transaction ts = null;
		String cityname = "";
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("select city from EcpBsTerritoryinfo where levelno=5 and territoryid=?");
			query.setParameter(0, territoryid);
			List<String> list = query.list();

			if (list.size() != 0)
				cityname = list.get(0);

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return cityname;
	}

	/**
	 * 根据购物中心id获取店铺品牌信息
	 */
	@SuppressWarnings("unchecked")
	public List<EcpGwcStore> selectStroe(String shopid) {// 三、getBrandShopInfoList接口
															// 十、getShoppingCenterBrandInfoList接口
															// 二十三、getBrandShopInfoList接口
		Session session = null;
		Transaction ts = null;
		List<EcpGwcStore> list = null;
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("from EcpGwcStore where shopid=?");
			query.setParameter(0, shopid);
			list = query.list();

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return list;

	}

	/**
	 * 根据购物中心id获取楼层字典
	 */
	@SuppressWarnings("unchecked")
	public List<String> selectFloor(String shopid) {// 十五、getShoppingCenterShopFloorInfoList接口
		Session session = null;
		Transaction ts = null;
		List<String> list = null;
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("select distinct floor from EcpGwcStore where shopid=?");
			query.setParameter(0, shopid);
			list = query.list();

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return list;

	}

	/**
	 * 根据楼层获取店铺信息
	 */
	@SuppressWarnings("unchecked")
	public List<EcpGwcStore> selectStoreByFloor(String floor) {// 十五、getShoppingCenterShopFloorInfoList接口
		Session session = null;
		Transaction ts = null;
		List<EcpGwcStore> list = null;
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session.createQuery("from EcpGwcStore where floor=?");
			query.setParameter(0, floor);
			list = query.list();

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return list;

	}

	/**
	 * 根据楼层获取楼层名称
	 */
	@SuppressWarnings("unchecked")
	public String selectFloorName(String floor) {// 十五、getShoppingCenterShopFloorInfoList接口

		Session session = null;
		Transaction ts = null;
		String floorname = "";
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("select dicname from EcpDicDetail where dicid=?");
			query.setParameter(0, floor);
			List<String> list = query.list();

			if (list.size() != 0)
				floorname = list.get(0);

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return floorname;
	}

	/**
	 * 返回搜索结果 专卖店列表
	 */
	@SuppressWarnings("unchecked")
	public List<ShopSort> selectStoreByAdd(String address, double longititue,
			double latitude) {// 二十三、getBrandShopInfoList接口
		Session session = null;
		Transaction ts = null;
		List<ShopSort> shopList = new ArrayList<ShopSort>();
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("from EcpGwcShopping where name like '%"
							+ address + "%' or address like '%" + address
							+ "%'");
			List<EcpGwcShopping> list = query.list();

			for (EcpGwcShopping ecpGwcShopping : list) {

				double distance = GetLongDistance(longititue, latitude,
						ecpGwcShopping.getLongitude(),
						ecpGwcShopping.getLatitude());// 单位M

				ShopSort shopSort = new ShopSort();
				shopSort.setShopid(ecpGwcShopping.getShopid());
				shopSort.setName(ecpGwcShopping.getName());
				shopSort.setLongitude(ecpGwcShopping.getLongitude());
				shopSort.setLatitude(ecpGwcShopping.getLatitude());
				shopSort.setDistance(distance);

				shopList.add(shopSort);

			}

			// 冒泡排序
			for (int i = 0; i < shopList.size(); i++) {
				for (int j = 0; j < shopList.size(); j++) {
					ShopSort temp;
					if (shopList.get(i).getDistance() < shopList.get(j)
							.getDistance()) {
						temp = shopList.get(j);
						shopList.set(j, shopList.get(i));
						shopList.set(i, temp);
					}
				}
			}

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return shopList;
	}

	/*
	 * 按标准的球面大圆劣弧长度计算，适用于距离较远的情况
	 */
	private double GetLongDistance(double lon1, double lat1, double lon2,
			double lat2) {
		double ew1, ns1, ew2, ns2;
		double distance;
		// 角度转换为弧度
		ew1 = lon1 * DEF_PI180;
		ns1 = lat1 * DEF_PI180;
		ew2 = lon2 * DEF_PI180;
		ns2 = lat2 * DEF_PI180;
		// 求大圆劣弧与球心所夹的角(弧度)
		distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1)
				* Math.cos(ns2) * Math.cos(ew1 - ew2);
		// 调整到[-1..1]范围内，避免溢出
		if (distance > 1.0)
			distance = 1.0;
		else if (distance < -1.0)
			distance = -1.0;
		// 求大圆劣弧长度
		distance = DEF_R * Math.acos(distance);
		return distance;
	}
}
